﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Supermarket;

namespace XLSupermarket.Storehouse.Commodity_Type
{
    public partial class Commodity_Type : Form
    {
        public Commodity_Type()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 查询功能
        /// </summary>
        private void Select() {
            string sql = "select * from Goods_Type";
            dataGridView1.DataSource = DBHelper.FillData(sql);
        }
        /// <summary>
        /// 清空功能
        /// </summary>
        private void Clear() {
            textBox1.Text = "";
            label2.Text = "添加时无需输入";
        }
        /// <summary>
        /// 加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Commodity_Type_Load(object sender, EventArgs e)
        {
            Select();
        }
        /// <summary>
        /// 单机数据表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string index = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string sql = "select * from Goods_Type where GoodsType_ID='"+index+"'";
            DataTable dt = DBHelper.FillData(sql);
            label2.Text = dt.Rows[0][0].ToString();
            textBox1.Text = dt.Rows[0][1].ToString();
        }
        /// <summary>
        /// 清空按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Clear();
        }
        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string sql = string.Format("update Goods_Type set GoodsType_Name='{0}' where GoodsType_ID='{1}'",textBox1.Text,label2.Text);
            int num = DBHelper.Edit(sql);
            if (num>0)
            {
                MessageBox.Show("修改成功！");
                Select();
            }
            else
            {
                MessageBox.Show("修改失败！");
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "insert into Goods_Type Values('" + textBox1.Text + "')";
                int num = DBHelper.Edit(sql);
                if (num > 0)MessageBox.Show("添加成功！");
                sql = "select * from Goods_Type where GoodsType_Name='" + textBox1.Text + "'";
                DataTable dt = DBHelper.FillData(sql);
                label2.Text = dt.Rows[0][0].ToString();
                textBox1.Text = dt.Rows[0][1].ToString();
                Select();
            }
            catch (Exception ex) {
                MessageBox.Show("您已存在该类型，无需再次添加！");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
